require 'libxml'
require 'faster_from_xml'
class XmlFile

    @@data = nil
    
    module Config
      mattr_accessor :current, :content_tag, :data
      @@current = "#{RAILS_ROOT}/db/current.xml"
      @@content_tag = 'xml-content'
    end 
  
  def self.data
    if @@data.nil?
      if File.exists?(XmlFile::Config.current)
        self.read_file
     else 
        self.write_file_from_db
        self.read_file
     end  
   end
   return @@data.to_hash["hash"]
  end
  
  def self.data_raw
    self.data unless @@data
    return @@data
  end
  
  def self.read_file
      parser = LibXML::XML::Parser.file(XmlFile::Config.current)
      doc = parser.parse    
      @@data =  doc.root    
  end
  
  def self.write_file_from_db #init only. comics are out of order.
      #recreate method (from db)
      data = {:volumes=>[]}
      if Volume.count == 0
        Volume.create
      end
      Volume.find(:all).each do |vol|        
        data[:volumes] << vol.to_hash
      end
      self.rewrite(data.to_xml)
      return true 
  end
  
  
  def self.write_db_from_file
    #update comics, chapters, stories, volumes
    #create comics, chapters, stories, volumes
    #delete comics, chapters, stories, volumes,
    #chapter_id's
    #story_ids
    #volume id's
  end
  
  
  def self.get_comic(i)
    return nil unless i
    self.data unless @@data  
    d = @@data.find("//comic[id=#{i}]")[0]
    if d
      return d.to_hash["comic"]
    end
    return nil
  end
  
  def self.insert_comic(comic)#called from comic.after_create
    self.data unless @@data
    chapter = self.find_chapter(comic.chapter_id)
    chapter.find("//comics")[0] << comic.xml_from_hash.copy("")
    self.rewrite(@@data)
  end  
  
  def self.insert_chapter(chapter)#called from chapter.after_create
    self.data unless @@data
    story = self.find_story(chapter.story_id)
    story.find("//chapters")[0] << chapter.xml_from_hash.copy("")
    self.rewrite(@@data)
  end   

  def self.update_comic(comic)
    self.data unless @@data
    old = self.find_comic(comic.id)
    #update attributes
    comic.attributes.each do |key,value|
       if n = old.find("//attributes/#{key.gsub("_","-")}")[0]
        n.content = value.to_s
      end
    end
    #update image and thumbnails
    old.find("//image")[0].content = comic.public_filename
    old.find("//thumbnails/small")[0].content = comic.public_filename(:small)
    old.find("//thumbnails/mini")[0].content = comic.public_filename(:mini)
    old.find("//thumbnails/large")[0].content = comic.public_filename(:large)
    old.find("//thumbnails/medium")[0].content = comic.public_filename(:medium)
    if comic.from_after_save #not tested yet
      if @@data.find("//chapter[id=#{comic.chapter_id}]//comic[id=#{comic.id}]")[0].nil?
        old.remove!
        @@data.find("//chapter[id=#{comic.chapter_id}]//comics")[0] << old
      end
    end    
    self.rewrite(@@data)    
  end

protected
  def self.rewrite(data)
    File.open(XmlFile::Config.current, "w") do |f|
        f.flock File::LOCK_EX
        f << data
    end    
  end
  def self.find_chapter(i)
    @@data.find("//chapter[id=#{i}]")[0]
  end
  def self.find_comic(i)
    @@data.find("//comic[id=#{i}]")[0]
  end 
    def self.find_story(i)
    @@data.find("//story[id=#{i}]")[0]
  end  
end
